<?php

namespace My\Model;

use Utuo\UtuoModel;
use Utuo\DBRW;

class DistributionShopRateModel extends UtuoModel {
	
	// 评价的状态
	const RATE_STATUS_UNRATE = 0; // 未评价
	const RATE_STATUS_BUYER = 1; // 买家已评完
	
	// 评价方
	const REVIEWER_BUYER = 0;
	
	// 审核
	const RATE_CHECK_NO = 0; // 未审核
	const RATE_CHECK_UNPASS = 1; // 未通过
	const RATE_CHECK_PASS = 2; // 通过
	
	public static $rateCheckStatus = array(
		self::RATE_CHECK_NO => '未审核',
		self::RATE_CHECK_UNPASS => '未通过',
		self::RATE_CHECK_PASS => '通过',
	);

	/**
	 * 获得新的评价状态
	 * @param unknown $rate_status 当前评价状态
	 * @param unknown $reviewer 评论方
	 * @return string
	 */
	public function getNextRateStatus($rate_status, $reviewer = self::REVIEWER_BUYER) {
		return self::RATE_STATUS_BUYER;
	}
	
	/**
	 * 提交评价内容
	 * @param unknown $order
	 * @param unknown $rates
	 * array(
	 * 		123(orderGoodsId) => "评价内容",
	 * 		124(orderGoodsId) => "评价内容",
	 * )
	 * @param unknown $uid
	 * @param unknown $reviewer
	 * @return boolean
	 */
	public function submitRate($order, $rates, $uid, $reviewer = DistributionShopRateModel::REVIEWER_BUYER) {
		if($order['rate_status'] == self::RATE_STATUS_BUYER) return false;
		/*添加评论数据*/
		foreach ($order["orderItems"] as $productId => $productItem) {
			foreach ($productItem["items"] as $item) {
				if(!array_key_exists( $item['orderGoodsId'], $rates) or M("DistributionShopRate")->where(array('order_id' => $order['id'], 'uid' => $uid, 'order_goods_id' => $item['orderGoodsId'], 'reviewer' => $reviewer,))->find()) continue; // 跳过不存在与已评价的订单商品
				$nextRateStatus = $this->getNextRateStatus();
				/*添加买家评论*/
				$addRates = array(
						'order_id' => $order['id'],
						'uid' => $uid,
						'product_id' => $productId,
						'order_goods_id' => $item['orderGoodsId'],
						'content' => $rates[$item['orderGoodsId']]["content"],
						'reviewer' => $reviewer,
						'status' => 0,
						'create_time' => time(),
				);
				M("DistributionShopOrderGoods")->where(array('order_id' => $order['id'], 'id' => $item['orderGoodsId']))->setField("rate_status", $nextRateStatus); // 设置订单商品评论状态
				if( false !== M("DistributionShopRate")->add( $addRates ) ) {
					// 提交成功
				} else {
					return false;
				}
			}
		}
		/*检查是否已评价完*/
		M("DistributionShopOrder")->where("id=%d", $order['id'])->setField("rate_status", 1); // 直接设置买家评价完成
		return true;
	}
	
	/**
	 * 验证评论
	 * @param unknown $order
	 * @param unknown $rates
	 * @return boolean
	 */
	public function checkRate( $order, $rates ) {
		DBRW::openAndClose(0);
		$data = "";
		/* 更新商家审核 */
		foreach ($order["orderItems"] as $productId => $productItem) {
			foreach ($productItem["items"] as $item) {
				if( !array_key_exists( $item['orderGoodsId'], $rates) ) continue; // 跳过不参与评论的订单商品
				$value = $rates[$item['orderGoodsId']]['status']*1;
				$data.=" WHEN {$item['orderGoodsId']} THEN {$value} ";
			}
		}
		if ( $data ) {
			if ( false !== M("DistributionShopRate")->where("order_id=%d", $order['id'])->save(array(
				'status' => array('exp', "CASE order_goods_id {$data} END"),
			)) ) {
				$count = M("DistributionShopRate")->where(array(
								"order_id" => $order['id'],
								"status" => array("eq", DistributionShopRateModel::RATE_CHECK_NO)
							), $order['id'])->count();	
				if ( !$count ) {
					M("DistributionShopOrder")->where("id=%d", $order['id'])->setField("rate_status", 2); // 直接设置卖家审核完成
				}
				DBRW::openAndClose(1);
				return true;
			} else {
				return false;
			}
		}
		return true;
	}
	
}